/*
 * SignatureForm.java
 *
 * Created on __DATE__, __TIME__
 */

package clientPresentation;

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author  __USER__
 */
@SuppressWarnings("serial")
public class SignatureForm extends javax.swing.JFrame {

    /** Creates new form SignatureForm */
    public SignatureForm() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    //GEN-BEGIN:initComponents
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        compField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        documentField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        durationField = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        veriCheckBox = new javax.swing.JCheckBox();

        setTitle("Assinar Documento");
        setDefaultCloseOperation(3);

        jButton1.setText("Assinar Documento");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText("Compet\u00eancia a Utilizar na Assinatura:");

        jLabel2.setText("Documento a ser assinado:");

        jLabel3.setText("Dura\u00e7\u00e3o pretendida (meses):");

        jButton2.setText("Escolher Ficheiro");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        veriCheckBox.setText("Utilizar Verifica\u00e7\u00e3o por VERICOP");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
                getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(
                        layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(
                                        layout.createParallelGroup(
                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                .addGroup(
                                                        layout.createSequentialGroup()
                                                                .addGroup(
                                                                        layout.createParallelGroup(
                                                                                javax.swing.GroupLayout.Alignment.TRAILING,
                                                                                false)
                                                                                .addComponent(
                                                                                        durationField)
                                                                                .addComponent(
                                                                                        jLabel3,
                                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                        Short.MAX_VALUE))
                                                                .addPreferredGap(
                                                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                                        44,
                                                                        Short.MAX_VALUE)
                                                                .addComponent(
                                                                        veriCheckBox))
                                                .addComponent(jLabel1)
                                                .addComponent(jLabel2)
                                                .addComponent(
                                                        compField,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        388, Short.MAX_VALUE)
                                                .addGroup(
                                                        layout.createSequentialGroup()
                                                                .addComponent(
                                                                        documentField,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                        260,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addPreferredGap(
                                                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                .addComponent(
                                                                        jButton2))
                                                .addComponent(jButton1))
                                .addContainerGap(
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        Short.MAX_VALUE)));
        layout.setVerticalGroup(layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(
                        layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jLabel1)
                                .addPreferredGap(
                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(compField,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(
                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel2)
                                .addPreferredGap(
                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(
                                        layout.createParallelGroup(
                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(
                                                        documentField,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(jButton2))
                                .addGap(18, 18, 18)
                                .addComponent(jLabel3)
                                .addPreferredGap(
                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(
                                        layout.createParallelGroup(
                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(
                                                        durationField,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(veriCheckBox))
                                .addGap(18, 18, 18).addComponent(jButton1)
                                .addContainerGap(14, Short.MAX_VALUE)));

        pack();
    }// </editor-fold>
    //GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

        // Abrir di�logo de 'Abrir Ficheiro'
        JFileChooser fc = new JFileChooser();
        int res = fc.showOpenDialog(this);

        // Se a opera��o falhou, abortar
        if (res != JFileChooser.APPROVE_OPTION)
            throw new RuntimeException(
                    "Falha ao tentar abrir o ficheiro escolhido");
        
        // Coloca o nome do ficheiro dentro da caixa de texto
        documentField.setText(fc.getSelectedFile().getAbsolutePath());
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

        // Test if the input is correct
        if(compField.getText().isEmpty() || documentField.getText().isEmpty()) {
            // Launch MessageDialog warning
            JOptionPane.showMessageDialog(this, 
                    "Deve especificar uma compet�ncia e escolher um documento");
            
            // Exit
            return;
        } else if (veriCheckBox.isSelected() && durationField.getText().isEmpty()) {
            // Launch MessageDialog warning
            JOptionPane.showMessageDialog(this, 
                    "Deve especificar uma dura��o para a assinatura," +
                    " se escolher utilizar o sistema VERICOP");
            
            // Exit
            return;
        } else if (veriCheckBox.isSelected()){
            // Test if duration is a number
            try {
                int i = Integer.parseInt(durationField.getText());
                
                if (i <= 0) {
                    throw new NumberFormatException();
                }
            } catch (NumberFormatException e) {
             // Launch MessageDialog warning
                JOptionPane.showMessageDialog(this, 
                        "Deve inserir um n�mero inteiro e maior que 0" +
                        " como dura��o");
                
                // Exit
                return;
            }
        }
        
        // Obter compet�ncia
        String comp = compField.getText();
        
        // Obter nome e abrir ficheiro
        File document = new File(documentField.getText());

        // Obter Dura��o pretendida para servi�o VERICOP
        Integer duration = 0;
        if (veriCheckBox.isSelected()) {
            duration = new Integer(durationField.getText());
        }
        
        // Abrir um dialog para come�ar o processo de assinatura
        SignatureDialog sd = new SignatureDialog(this, false, comp,
                document, duration, veriCheckBox.isSelected());
        sd.setVisible(true);
        sd.trySignature();
        

        // If the operation succeeds, close the dialog
        sd.setVisible(false);
        sd.dispose();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new SignatureForm().setVisible(true);
            }
        });
    }

    //GEN-BEGIN:variables
    // Variables declaration - do not modify
    private javax.swing.JTextField compField;

    private javax.swing.JTextField documentField;

    private javax.swing.JTextField durationField;

    private javax.swing.JButton jButton1;

    private javax.swing.JButton jButton2;

    private javax.swing.JLabel jLabel1;

    private javax.swing.JLabel jLabel2;

    private javax.swing.JLabel jLabel3;

    private javax.swing.JCheckBox veriCheckBox;
    // End of variables declaration//GEN-END:variables

}